package com.wk1un2.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wk1un2.entity.Initaccess;
import com.wk1un2.entity.Initmain;
import com.wk1un2.entity.Menus;
import com.wk1un2.service.MenusService;
import com.wk1un2.util.Result;

@RequestMapping("/public")
@RestController
public class MenusController {
	@Autowired
	private MenusService menusservice;

	@RequestMapping("/getAllMenus")
	public Object getAll(@RequestParam Map<String, Object> map) {
		Result res = new Result(0, "操作成功", menusservice.count(map), menusservice.getAllMenus(map));
		System.out.println("==========>" + res);
		return res;
	}

	
	@RequestMapping("/addMenus")
	public Object add(@RequestParam Map<String, Object> map) {
		Result res=null;
		if (menusservice.getMenusName(map).size()>0) {
			res = new Result(0, "添加失败，相同模块下模块名称不能重复", -1, false);
		} else {
			res = new Result(0, "操作成功", menusservice.add(map), true);
		}
		return res;
	}

	
	@RequestMapping("/updateMenus")
	public Object update(@RequestParam Map<String, Object> map) {
           Result res=null;
           System.out.println("==>>>>修改模块获取map>>"+map);
		if (menusservice.getMenusName(map).size()>0) {
			res = new Result(0, "修改失败，相同模块下模块名称不能重复", -1, false);
		} else {
			res = new Result(0, "操作成功", menusservice.update(map), true);
		}
		return res;
	}

	@RequestMapping("/deleteMenus")
	public Object delete(Integer menuid) {
		//Result res = new Result(0, "操作成功", menusservice.delete(menuid), true);
		
		System.out.println(menuid);
		int j = menusservice.getRolesCount(menuid);
		System.out.println("######################################");
		System.out.println(j);
		if (j > 0) {
			Result res = new Result(0, "删除失败,模块关联的有角色！！！", 0, true);
			return res;
		} else {
			int i = menusservice.delete(menuid);
			Result res = new Result(0, "请求成功!", i, true);
			return res;
		}
	}
	
	
	
	
	
	
	
	
	
	@RequestMapping("/getRolesMenus")
	public Object getRolesMenus(Integer userid) {
		Map<String, String> homeinfo = new HashMap<>();
		Map<String, String> logoInfo = new HashMap<>();
		List menuInfo = menusservice.getRolesMenus(userid);
		homeinfo.put("title", "首页");
		homeinfo.put("href", "page/welcome-1.html?t=1");
		logoInfo.put("title", "Authority");
		logoInfo.put("image", "images/logo.png");
		logoInfo.put("href", "");
		Initmain im = new Initmain();
		List lsss = new ArrayList<>();
		Initaccess ia = new Initaccess();
		ia.setTitle("常规管理");
		ia.setIcon("fa fa-address-book");
		ia.setTarget("_self");
		ia.setChild(menuInfo);
		lsss.add(ia);
		im.setHomeInfo(homeinfo);
		im.setLogoInfo(logoInfo);
		im.setMenuInfo(lsss);
		return im;
	}

}
